﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pixcom
{
  
    public partial class DodemServerForm : Form 
    {
        private AsynchronousSocketListener asyncServer;
        private TreeNode ptree=null;//根节点
        Pixcom_Form pxform=null;


        public DodemServerForm()
        {
            InitializeComponent();
 
            asyncServer = new AsynchronousSocketListener(SafeupdateUI);
            pxform = new Pixcom_Form(asyncServer,null);
            pxform.FormBorderStyle = FormBorderStyle.None;
            pxform.MdiParent = this;
          
            pxform.Dock = DockStyle.Fill;
            pxform.Show();
            treeEqpt.Nodes.Clear();
            treeEqpt.SelectedImageIndex = -1;
         
            ptree =treeEqpt.Nodes.Add("设备列表");
            ptree.SelectedImageIndex = -2;
            ptree.SelectedImageKey = null;
            ptree.Nodes.Add("0", String.Format("设备{0}", 0), PicProtocal.OFFLINE_IMG,null);
            for (int i = 0; i < 255; i++) {
               // ptree.Nodes.Add(String.Format("设备{0}", i)).ImageKey = "offline.png";
             //   ptree.Nodes.Add(i.ToString(), String.Format("设备{0}", i),PicProtocal. OFFLINE_IMG);
            }
            //展开所有设备树
            ptree.ExpandAll();
        }

        private void SafeupdateUI(object state)
        {

             
             ImageObject imgobj = (ImageObject)state;
             try
               {
                    if (imgobj.Equals(pxform.imgobj)) {
                        pxform.SafeupdateUI(imgobj);
                    }
 
              }
                catch (Exception e)
                {

                   Logger.Error(e.ToString());
                    return;
                }
 
                if (imgobj.PackageType == PicProtocal.RSACK_ASK_PARA)
                {

                    String eqpid = imgobj.eqpid.ToString();

                    TreeNode[] nodes = ptree.Nodes.Find(eqpid, true);

                    if (nodes.Length > 0)
                    {
                        nodes[0].ImageKey = PicProtocal.ONLINE_IMG;
                    }
                    else {
                    TreeNode node= ptree.Nodes.Add(eqpid, String.Format("设备{0}", eqpid), PicProtocal.ONLINE_IMG);
                    
                    }

                }
            
             
             
        }
 

        private void DodemServerForm_Load(object sender, EventArgs e)
        {
            
          
           asyncServer.StartListening();
        }

        private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void DodemServerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
           
            asyncServer.stop();
            System.Threading.Thread.Sleep(1000);


        }

        private void DodemServerForm_FormClosed(object sender, FormClosedEventArgs e)
        {
         //  MessageBox.Show("退出当前窗体？", "", MessageBoxButtons.OK);
            Application.Exit();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void treeEqpt_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //双击设备 查看设备属性
           

        }

        private void splitter1_SplitterMoved_1(object sender, SplitterEventArgs e)
        {

        }

        private void treeEqpt_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
          
        }

        private void treeEqpt_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent == null)
            {

                treeEqpt.SelectedNode = null;
            }
            else
            {
            }
        }

        private void treeEqpt_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //   MessageBox.Show(e.Node.Text+"===="+e.Node.Name);
            ImageObject imgobj =   asyncServer.getImgObj(e.Node.Name);

            if (pxform.Visible) {
                if (imgobj != null)
                {
                    Logger.Info("更新调试窗口SOket信息,{0}", imgobj.eqpid);
                    pxform.updateSocket(imgobj);
             
                }
                else {
                    MessageBox.Show("设备不存在", " 错误", MessageBoxButtons.OK);
                }
             
            }
        }
    }
}
